﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using XRGLib;
using Console = XRGLib.Console;

namespace Examples
{
    class Example2 : ExampleBase
    {
        Surface offScreen;
        Point currentPos;

        public Example2(Console console, int width, int height, Game game)
            : base(console, width, height, game)
        {
            offScreen = console.CreateSurface(20, 10);
            currentPos = new Point(5, 30);

            DrawOffscreen();
        }

        public override string Description
        {
            get { return "Offscreen console & blitting"; }
        }

        float shift;
        int elapsedMilli = 0;
        Point delta = new Point(1, 1);

        public override void Update(GameTime gameTime)
        {
            elapsedMilli += gameTime.ElapsedGameTime.Milliseconds;
            if (elapsedMilli >= 500)
            {
                elapsedMilli = 0;

                currentPos.X += delta.X;
                currentPos.Y += delta.Y;

                if (currentPos.X >= surface.Width)
                {
                    delta.X = -1;
                }
                if (currentPos.X <= -offScreen.Width)
                {
                    delta.X = 1;
                }
                if (currentPos.Y >= surface.Height)
                {
                    delta.Y = -1;
                }
                if (currentPos.Y <= -offScreen.Height)
                {
                    delta.Y = 1;
                }
            }
        }

        public override Surface ReDraw()
        {
            shift += 0.5f;
            for (int y = 0; y < surface.Height; y++)
            {
                for (int x = 0; x < surface.Width; x++)
                {
                    float hue = shift + (x + y);
                    Color col = ColorHelper.CreateFromHSV(hue, 1, 1);

                    char c = (char)rand.GetRandomCharacter("!@#$%^&*()");
                    surface.PrintChar(x, y, c, Color.Black, col);
                }
            }
            console.BlitAlpha(offScreen, surface, offScreen.Rect, currentPos.X, currentPos.Y, .75f);
            return base.ReDraw();
        }

        void DrawOffscreen()
        {
            offScreen.DrawFrame(offScreen.Rect, null, true, Color.Violet, Color.Black);

            offScreen.PrintStringRect(new Rectangle(1,1,offScreen.Width-2,offScreen.Height-2),
                "This is an off-screen surface, blitted over another surface using simulated alpha transparency",
                HorizontalAligment.Left, VerticalAlignment.Top, WrappingType.Word);
        }
    }
}